package graduate.cslg.com.universityshopping.bbs;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.adapter.BBSItemAdapter;
import graduate.cslg.com.universityshopping.adapter.BBSReplyAdapter;
import graduate.cslg.com.universityshopping.entity.ReplyBean;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BBSContentActivity extends AppCompatActivity {

    private int id;
    private RecyclerView mRecyclerView;
    private List<ReplyBean> mReplyBeans;
    private BBSReplyAdapter mBBSReplyAdapter;
    private String userName;
    private String bbsContent;
    private String bbsTitle;
    private String postTime;
    private String bbsType;
    private TextView user_name;
    private TextView post_time;
    private TextView bbs_type;
    private TextView bbs_content;
    private TextView bbs_title;
    private EditText postContent;
    private Button postButton;
    private LoginResponse mResponse;
    private String userAccount;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbscontent);

        initViews();
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        Intent intent = getIntent();
        id = intent.getIntExtra(BBSItemAdapter.BBS_ID, 0);
        userName = intent.getStringExtra("user_name");
        bbsContent = intent.getStringExtra("bbs_content");
        bbsTitle = intent.getStringExtra("bbs_title");
        postTime = intent.getStringExtra("post_time");
        bbsType = intent.getStringExtra("bbs_type");
        user_name.setText(userName);
        post_time.setText(postTime);
        bbs_content.setText(bbsContent);
        bbs_title.setText(bbsTitle);
        bbs_type.setText(bbsType);
        getBBSDetail(id);
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        userAccount = preferences.getString("user_account", "Z09315136");

        postButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = postContent.getText().toString();
                if (content.length() == 0) {
                    Toast.makeText(BBSContentActivity.this, "请输入正确的回复内容！", Toast.LENGTH_SHORT).show();
                } else {
                    replyPost(id, userAccount, getNowTime(), content);
                }
            }
        });
    }

    private void initViews() {
        mRecyclerView = findViewById(R.id.reply_info_recycler);
        user_name = findViewById(R.id.user_name);
        post_time = findViewById(R.id.post_time);
        bbs_type = findViewById(R.id.bbs_type);
        bbs_content = findViewById(R.id.bbs_content);
        bbs_title = findViewById(R.id.bbs_title);
        postButton = findViewById(R.id.submit_button);
        postContent = findViewById(R.id.post_content);
        mToolbar = findViewById(R.id.toolbar);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private void getBBSDetail(int bbs_id) {
        RetrofitDevelop.getTestService()
                .get_reply_info(bbs_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ReplyBean>>() {
                    @Override
                    public void onCompleted() {
                        mBBSReplyAdapter = new BBSReplyAdapter(mReplyBeans);
                        mRecyclerView.setAdapter(mBBSReplyAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(BBSContentActivity.this, "网络异常！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<ReplyBean> replyBeans) {
                        mReplyBeans = replyBeans;
                    }
                });
    }

    public static String getNowTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    private void replyPost(int bbsId, String replyName, String replyTime, String replyContent) {
        RetrofitDevelop.getTestService()
                .replyPost(bbsId, replyName, replyTime, replyContent)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mResponse.getStatus().equals("true")) {
                            postContent.setText(null);
                            getBBSDetail(id);
                            Toast.makeText(BBSContentActivity.this, "回复成功！", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(BBSContentActivity.this, "数据错误，请稍后再试！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(BBSContentActivity.this, "网络错误，请稍后再试！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mResponse = loginResponse;
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
